﻿using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;

namespace AgileDevTool.Helpers
{
    public class ZipHelper
    {
        public static byte[] ZipSingleFile(string password, params string[] files)
        {
            using (var ms = new MemoryStream())
            {
                using (var s = new ZipOutputStream(ms)
                {
                    Password = password
                })
                {
                    s.SetLevel(9);

                    var buffer = new byte[4096];
                    foreach (string file in files)
                    {
                        var entry = new ZipEntry(Path.GetFileName(file))
                        {
                            DateTime = DateTime.Now
                        };
                        s.PutNextEntry(entry);
                        using (var fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }

                    s.Finish();
                    s.Close();
                    return ms.ToArray();
                }
            }
        }

        public static void ZipDirectory(string sourceDirectoryName, string destinationArchiveFileName)
        {
            System.IO.Compression.ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName);
        }

        public static byte[] UnZipSingleFile(string zipedFile, string password)
        {
            using (var s = new ZipInputStream(File.OpenRead(zipedFile))
            {
                Password = password
            })
            {
                var entry = s.GetNextEntry();
                var bytes = new byte[entry.Size];
                s.Read(bytes, 0, bytes.Length);
                return bytes;
            }
        }

        public static byte[] UnGzipSingleFile(string zipedFile)
        {
            using (var gz = new GZipInputStream(File.OpenRead(zipedFile)))
            {
                using (var ms = new MemoryStream())
                {
                    int count;
                    var data = new byte[1024 * 1024];
                    while ((count = gz.Read(data, 0, data.Length)) != 0)
                    {
                        ms.Write(data, 0, count);
                    }

                    return ms.ToArray();
                }
            }
        }

        public static byte[] UnGzipSingleFile(byte[] bytes)
        {
            using (var input = new MemoryStream(bytes))
            {
                using (var gz = new GZipInputStream(input))
                {
                    using (var ms = new MemoryStream())
                    {
                        int count;
                        var data = new byte[1024 * 1024];
                        while ((count = gz.Read(data, 0, data.Length)) != 0)
                        {
                            ms.Write(data, 0, count);
                        }

                        return ms.ToArray();
                    }
                }
            }
        }
    }
}
